package cn.jhc.sevlet;//添加缴费信息

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jhc.bean.Clazz;
import cn.jhc.bean.Course;
import cn.jhc.bean.Payment;
import cn.jhc.bean.Score;
import cn.jhc.bean.Student;
import cn.jhc.dao.ClassDao;
import cn.jhc.dao.CourseDao;
import cn.jhc.dao.PaymentDao;
import cn.jhc.dao.ScoreDao;
import cn.jhc.dao.StudentDao;
import cn.jhc.dao.impl.ClassDaoImpl;
import cn.jhc.dao.impl.CourseDaoImpl;
import cn.jhc.dao.impl.PaymentDaoImpl;
import cn.jhc.dao.impl.ScoreDaoImp;
import cn.jhc.dao.impl.StudentDaoImpl;

/**
 * Servlet implementation class AddpaymentServlet
 */
@WebServlet("/user/teacher/addpayment.do")
public class AddpaymentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddpaymentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession();
		String sno = request.getParameter("sno");
		StudentDao studentDao = new StudentDaoImpl();
		Student student = studentDao.getStudent(sno);
		
		boolean flag;
		try{
			BigDecimal money = new BigDecimal(request.getParameter("money"));
			String paynumber = request.getParameter("paynumber");
			String remark = request.getParameter("remark");
			Payment payment = new Payment();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");	
			Date paytime = sdf.parse(request.getParameter("paytime"));
			payment.setTime(paytime);
			payment.setStudent(student);
			payment.setMoney(money);
			payment.setNumber(paynumber);
			payment.setRemark(remark);
		
			PaymentDao paymentDao = new PaymentDaoImpl();	
			paymentDao.save(payment);
			flag = true;
		} catch(Exception e){
			flag = false;
		}
		
		if(flag){
			response.sendRedirect("./tpayment.do");
		}
		else{
			request.setAttribute("flag", false);
			request.getRequestDispatcher("addpayment.jsp").forward(request,
					response);
		}
		
	}

}
